import type { ActionType, ProColumns } from '@ant-design/pro-components';
import { ProTable } from '@ant-design/pro-components';
import { useEffect, useImperativeHandle, useRef, useState } from 'react';
import { Button, message } from 'antd';
import { getLogManageCardList } from '@/pages/DoorOpeningLog/CardProduction/Administer/request';
import { ReportLostAdminCard } from './request';
import { useDeleteConfirm } from '@/hooks/useAlartMolder';
import { useDetermineCardType } from '@/hooks/DetermineCardType';
import React from 'react';
import { useIntl } from '@umijs/max';
export const waitTimePromise = async (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};

export const waitTime = async (time: number = 100) => {
  await waitTimePromise(time);
};
type GithubIssueItem = {
  name: string;
  room: string;
  type: string;
  faka: string;
  id: string
  state: string;
  comments: number;
  created_at: string;
  updated_at: string;
  closed_at?: string;
  cardType?: string
  cardHolder: string
};

interface Prpos {
  setsendMsg: any
  setType: any
  value: any
}
const LostCardTable: React.FC<Prpos> = ({ value, setsendMsg, setType }, ref) => {




  const intl = useIntl()

  let [cardId, setcardId] = useState('')

  useEffect(() => {

    switch (value) {
      case "mastercard":
        setcardId("2")
        break;
      case "floorcard":
        setcardId("4")
        break;
      case "buildingcard":
        setcardId("3")
        break;
      case "emergencycard":
        setcardId("6")
        break;
      case "staffcard":
        setcardId("7")
        break;
      case "personnelcard":
        setcardId("10")
        break;
      case "setvipcard":
        setcardId("18")
        break;
    }

    actionRef.current?.reload()

  }, [value])

  const reload = () => {
    actionRef.current?.reload()
  }

  useImperativeHandle(ref, () => ({
    reload
  }));


  const actionRef = useRef<ActionType>();

  const columns: ProColumns<GithubIssueItem>[] = [
    {
      title: intl.formatMessage({ id: 'Table.serial' }),
      dataIndex: 'inedx',
      valueType: 'index',

    },
    {
      title: intl.formatMessage({ id: "Table.cardholder" }),
      dataIndex: 'cardHolder',
    },
    {
      title: intl.formatMessage({ id: "card.Card type" }),
      dataIndex: 'cardType',
      valueType: 'select',
      valueEnum: {
        2: {
          text: intl.formatMessage({ id: "card.Total card" }),
        },
        3: {
          text: intl.formatMessage({ id: "card.Building card" }),
        },
        4: {
          text: intl.formatMessage({ id: "card.Layer control card" }),
        },
        6: {
          text: intl.formatMessage({ id: "card.Layer control card" }),
        },
        7: {
          text: intl.formatMessage({ id: "card.Hygiene card" }),
        },
        8: {
          text: intl.formatMessage({ id: "card.Always-on card" }),
        },
        10: {
          text: intl.formatMessage({ id: "card.Employee card" }),
        },
        18: {
          text:intl.formatMessage({ id: "card.Authorization card" }),
        },
      },
    },
    // {
    //   title: '卡状态',
    //   dataIndex: 'status',
    //   valueEnum: {
    //     1: {
    //       text: '在用',
    //     },
    //     2: {
    //       text: '退卡',
    //     },
    //     3: {
    //       text: '强制退卡',
    //     },
    //     4: {
    //       text: '挂失',
    //     },
    //   },
    // },
    {
      title: intl.formatMessage({id:"Table.Card issuance time"}),
      dataIndex: 'issuingTime',
      search: false,
    },
    {
      title: intl.formatMessage({id:"Lost.Expiration date"}),
      dataIndex: 'expirationDate',
      search: false,
    },
    {
      title: intl.formatMessage({id:'card.number'}),
      dataIndex: 'cardNum',
      search: false,
    },
    {
      title:intl.formatMessage({id:"Table.Building"}),
      dataIndex: 'buildingName',
      search: false,
    },
    {
      title:intl.formatMessage({id:"Table.Floor"}),
      dataIndex: 'floor',
      search: false,
      width: 430
    },
    {
      title: intl.formatMessage({id:"Table.Card issuer"}),
      dataIndex: 'createdBy',
      search: false,
    },
    // {
    //   title: '操作',
    //   align: 'center',
    //   valueType: 'option',
    //   key: 'option',
    //   search: false,
    //   // eslint-disable-next-line @typescript-eslint/no-unused-vars
    //   render: (_text: any, record: GithubIssueItem) => (
    //     <>
    //       <Button type="link" onClick={ () => {
    //         ReportLostCard(record)
    //       }}>
    //         挂失
    //       </Button>

    //     </>
    //   ),
    // },
  ];


  const ReportLostCard = async (result: GithubIssueItem) => {
    let [str] = useDetermineCardType("", "", result.cardType)
    useDeleteConfirm(`${result.cardHolder}的${str}确定要挂失吗?`, "", async (res) => {
      if (res == true) {
        let data = await ReportLostAdminCard(result.id)
        if (data.code == 0) {
          message.success(data.msg)
          actionRef.current?.reload();
        } else {
          message.error(data.msg)
        }
      }
    })

  }


  const reTurnadmin = () => {
    let data = { "cardclass": "readcard", "uuid": "12345", "isclear": "0" }
    setsendMsg(data)
    setType("读卡成功1")
  }


  return (
    <>

      <ProTable<GithubIssueItem>
        defaultSize={'large'}
        columns={columns}
        actionRef={actionRef}
        cardBordered={false}
        pagination={{
          defaultPageSize: 10,
          showSizeChanger: true,
        }}
        request={async (params) => {
          await waitTime(500);
          const res: any = await getLogManageCardList({
            ...params,
            page: params.current,
            status: 1,
            cardType: cardId
          });
          return {
            data: res.data.list || [],
            success: res.code === 0 ? true : false,
            total: res.data.totalCount,
          };
        }}
        rowKey="id"
        search={false}
        options={{
          setting: {
            listsHeight: 400,
          },
        }}
        dateFormatter="string"
        headerTitle={<span style={{ fontWeight: 'bold' }}>{intl.formatMessage({id:"Table.Effective management card"})}</span>}
      />
    </>
  );
};

export default React.forwardRef(LostCardTable);
